package com.example.demo.mapper;

import com.example.demo.entity.StudentCourse;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface StudentCourseMapper {
    // 新增学生选课记录
    @Insert("INSERT INTO student_courses (student_id, course_id) VALUES (#{student_id}, #{course_id})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertStudentCourse(StudentCourse studentCourse);

    // 删除学生选课记录
    @Delete("DELETE FROM student_courses WHERE id = #{id}")
    int deleteStudentCourseById(int id);

    // 修改学生选课记录
    @Update("UPDATE student_courses SET student_id = #{student_id}, course_id = #{course_id} WHERE id = #{id}")
    int updateStudentCourse(StudentCourse studentCourse);

    // 查询学生选课记录
    @Select("SELECT * FROM student_courses WHERE id = #{id}")
    StudentCourse selectStudentCourseById(int id);

    // 查询所有学生选课记录
    @Select("SELECT * FROM student_courses")
    List<StudentCourse> selectAllStudentCourses();

    // 联合查询学生及其选课信息
    @Select("SELECT sc.* FROM student_courses sc JOIN students s ON sc.student_id = s.id WHERE s.id = #{studentId}")
    List<StudentCourse> selectStudentCoursesByStudentId(int studentId);

    // 联合查询课程及其选课学生信息
    @Select("SELECT sc.* FROM student_courses sc JOIN courses c ON sc.course_id = c.id WHERE c.id = #{courseId}")
    List<StudentCourse> selectStudentCoursesByCourseId(int courseId);
}